package com.llx.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.llx.domain.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.io.IOException;

/**
 * @version 1.0
 * @description
 * @author viki
 * @date 2021/8/20 21:04
 */
public class WorkQueueMessageListener01 implements MessageListener {

    // 这个对象的作用是把json的字符串转换为实体类对象
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onMessage(Message message) {
        // 1. 获取信息
        byte[] body = message.getBody();
        // 2. 把消息转换为User对象
        try {
            User user = objectMapper.readValue(body,User.class);
            System.out.println("消费者1: " + user);
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
}
